<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * 
 *
 * @icon user
 */
class Userallinfo extends Backend
{
    
    /**
     * User模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');

    }


    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($list as $key=>$val){
                $first_tender = Db::name('borrow_tender')->field('account,createTime')->where(['userId'=>$val['userId'],'isFirst'=>'1'])->find();
                $tendertotal = Db::name('borrow_tender')->field('SUM(account) as total')->where(['userId'=>$val['userId']])->find();
                $repaytotal = Db::name('tender_repay')->field('SUM(totalAccount) as total')->where(['userId'=>$val['userId']])->where('status !=2')->find();
                $cashtotal = Db::name('cash')->field('SUM(money) as total')->where(['userId'=>$val['userId']])->find();

                $list[$key]['cashtotal'] = $cashtotal['total']?$cashtotal['total']:0;
                $list[$key]['cashcount'] = Db::name('cash')->where(['userId'=>$val['userId']])->count();;
                $list[$key]['repay'] = $repaytotal['total']?$repaytotal['total']:0;
                $list[$key]['firsttender_time'] = $first_tender['createTime']?$first_tender['createTime']:'';
                $list[$key]['tender_total'] = $tendertotal['total']?$tendertotal['total']:0;
                $list[$key]['tender_count'] = Db::name('borrow_tender')->where(['userId'=>$val['userId']])->count();
                $list[$key]['firsttender_account'] = $first_tender['account']?$first_tender['account']:0;

            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }



    public function detail($ids = NULL)
    {
        $row = $this->model->get($ids);

        $account = Db::name('user_account')->where('userId',$row['userId'])->find();
        $repay = DB::name('tender_repay')->field('repayTime')->where('status !=2 and userId='.$row['userId'])->order('repayTime asc')->find();
        $row['repayTime'] = $repay['repayTime'];

        $tenderlist = Db::name('borrow_tender')->alias('a')->field('a.tenderId,b.name as borrowname,u.phone,u.name,b.account as borrowaccount,b.aprTotal,b.isDay,b.time,a.account,a.interest,
                    a.totalAccount,a.createTime,b.fullTime,a.cardId,a.device')->join('borrow b','a.borrowId=b.borrowId')->join('user u','b.userId=u.userId')->where('a.userId',$row['userId'])->where('a.status > 0')->group('a.tenderId')->order('a.tenderId desc')->select();
            //->join('tender_repay t','a.tenderId = t.tenderId')
        foreach($tenderlist as $key=>$val){
            $repay = Db::name('tender_repay')->field('repayTime,addApr')->where('tenderId',$val['tenderId'])->find();
            $tenderlist[$key]['repayTime'] = $repay['repayTime'];
            $tenderlist[$key]['note'] = '';
            if($repay['addApr']>0){
                $tenderlist[$key]['note'] = '使用'.$repay['addApr'].'%加息卡';
            }
            if($val['cardId']>0){
                $cards  = Db::name('cards')->field('money')->where('cardId',$val['cardId'])->find();
                $tenderlist[$key]['note'] = '使用'.$cards['money'].'元红包';
            }
        }

        $cashlist = Db::name('cash')->where('userId',$row['userId'])->select();




        foreach($cashlist as $k=>$v){
            if($v['status']==0){
                $cashlist[$k]['status1'] = '待处理';
            }else if($v['status']==1){
                $cashlist[$k]['status1'] = '处理中';
            }else if($v['status']==2){
                $cashlist[$k]['status1'] = '已完成';
            }
        }


        $tradelist = Db::name('trade')->where('accountId',$row['jxAccount'])->order('reldate desc,inptime desc')->select();
        foreach($tradelist as $key=>$val){
            $year = substr($val['reldate'],0,4);
            $month = substr($val['reldate'],4,2);
            $day = substr($val['reldate'],6,2);
            $hour = substr($val['inptime'],0,2);
            $miniter = substr($val['inptime'],2,2);
            $second = substr($val['inptime'],4,2);
            $tradelist[$key]['inpdate'] =  $year.'-'.$month.'-'.$day .' '.$hour .':'.$miniter.':'.$second;
            $tradename  = Db::name('trade_type')->where('id',$val['type'])->find();
            $tradelist[$key]['typename'] = $tradename['name'];
        }



        $this->view->assign('tradelist',$tradelist);
        $this->view->assign('cashlist',$cashlist);
        $this->view->assign('tenderlist',$tenderlist);
        $this->view->assign('row',$row);
        $this->view->assign('account',$account);
        return $this->view->fetch();



    }

}
